package com.d5xx.hunters.API;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import static com.d5xx.hunters.Variables.*;

@SuppressWarnings("unused")
public class HunterPlayer{
    private Player player; //Bukkit玩家类型
    private String type; //玩家类型(lobby, lobby-waiting, game, game-fall, death)
    private String name; //玩家名
    private UUID playerUUID; //玩家UUID
    private BukkitRunnable scoreBoard; //玩家计分板
    private YamlConfiguration langCon; //玩家语言文件
    private Map<String, Object> settingsMap; //玩家设置

    public HunterPlayer() {
    }

    public HunterPlayer(Player player, String type) {
        this.player = player;
        this.type = type;
        playerUUID = player.getUniqueId();
        settingsMap = Hunter.getPlayerSettings(playerUUID);
        langCon = Hunter.getLanguageCon((String) settingsMap.get("language"));
        Hunter.addPlayer(player, this);
    }

    public Player getPlayer() {
        return player;
    }

    public void setPlayer(Player player) {
        this.player = player;
    }

    public YamlConfiguration getLangCon() {
        return langCon;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BukkitRunnable getScoreBoard() {
        return scoreBoard;
    }

    public void setScoreBoard(BukkitRunnable scoreBoard) {
        if (this.scoreBoard != null) {
            this.disableScoreboard();
        }
        this.scoreBoard = scoreBoard;
    }

    public Map<String, Object> getSettingsMap() {
        return settingsMap;
    }

    public void disableScoreboard() {
        this.scoreBoard.cancel();
    }

    public void enableScoreboard() {
        this.scoreBoard.runTaskTimerAsynchronously(plugin, 0L, 10L);

    }

    public void sendKeyMessage(String key, boolean prefix) {
        String message = langCon.getString(key);
        if (message == null) {
            return;
        }
        if (prefix) {
            String pre = langCon.getString("plugin-prefix");
            if (pre != null) {
                message = pre + message;
            }
        }
        player.sendMessage(message);
    }

    public String getKeyMessage(String key) {
        return langCon.getString(key);
    }

    public List<String> getKeyListMessage(String key) {
        return langCon.getStringList(key);
    }

    public UUID getPlayerUUID () {
        return playerUUID;
    }

    public Object getPlayerSetting(String key) {
        return settingsMap.get(key);
    }

    public void setPlayerSetting(String key, Object value) {
        settingsMap.replace(key, value);
        switch (key) {
            case "language":
                langCon = Hunter.getLanguageCon((String) value);
                break;
        }
    }

    public void sendActionBar(String text) {
        player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(text));
    }
}
